package studentinformationmanagementsystem.service.impl;

import studentinformationmanagementsystem.dao.TeacherDao;
import studentinformationmanagementsystem.dao.TeachersSchedulesDao;
import studentinformationmanagementsystem.dao.impl.ClassandgradDaolmpl;
import studentinformationmanagementsystem.dao.impl.TeacherDaoImpl;
import studentinformationmanagementsystem.dao.impl.TeachersSchedulesDaolmpl;
import studentinformationmanagementsystem.dao.impl.TemporaryClassTableDaolmpl;
import studentinformationmanagementsystem.entity.*;
import studentinformationmanagementsystem.service.TeacherService;

import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class TeacherServiceImpl implements TeacherService {

      TeacherDao  teacherDao=new TeacherDaoImpl();
    /**
     * 登陆
     *
     * @param teacher
     * @return
     */
    @Override
    public Teacher login(Teacher teacher) {
        return teacherDao.queryByUsernameAndPassword(teacher);
    }


    /**
     * 检查教师工号是否已存在
     *
     * @param teacher
     * @return true 教师工号已存在
     */
    @Override
    public boolean isExistsUsername(Teacher teacher) {

        boolean falg=false;

        Teacher teacher1 = teacherDao.queryByUsername(teacher);

        if (teacher1!=null){
            falg=true;
        }

        return falg;
    }

    /**
     * 分页查询
     * @param pageNo  查询页码
     * @param pageSize  每页的记录数
     * @return
     */
    @Override
    public Page<Teacher> page(int pageNo, int pageSize) {
        /**
         * 当前页码:pageNo
         * 总页码:pageTotal
         * 当前页显示的数量:pageSize
         * 总记录数:pageTotalCount
         * 当前页数据:items
         */

        //访问页面的记录条数的开始行数：当前页码-1*每页展示的记录行数
        int bigin=(pageNo-1)*pageSize;


        //总记录数量
        Integer pageTotalCount=teacherDao.queryForPageTotalCount();

        //总页码数量=总记录数/每页记录显示数量，然后判断是否可以被除尽，如果不能除尽，那么总的页码数量就需要+1
        Integer pageTotal=pageTotalCount/pageSize;

        if (pageTotalCount%pageSize!=0){
            pageTotal++;
        }


        //当前页码数据
        List<Teacher> items = teacherDao.queryForPageItems(bigin, pageSize);

        Page<Teacher> objectPage = new Page<Teacher>(pageNo,pageTotal,pageSize,pageTotalCount,items);

        return objectPage;
    }

    /**
     * 添加教师信息用户
     *
     * @param teacher
     */
    @Override
    public int register(Teacher teacher) {
        teacher.setIsDelete(0);
        return teacherDao.insert(teacher);
    }

    /**
     *  根据ID更新教师信息
     * @param teacher  教师对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Teacher teacher) {
        return teacherDao.update(teacher);
    }



    /**
     *  根据ID删除教师
     * @param teacher 教师ID
     * @return 受影响的记录行数
     * 逻辑思路：
     * 1.使用要删除的信息来查询教师信息，获取这个教师信息，修改其是否修改标志
     * 2.是否有此教师id查询教师任课表，将此条信息修改其标志
     * 3.选课临时表包含教师任课表的id，将此条信息修改其标志
     * 4.删除学生选课表
     * 5.学生必修课表
     * 6.学生课表
     */

    @Override
    public int delete(Teacher teacher) {

        Teacher teacher1 = queryById(teacher);
        teacher1.setIsDelete(1);   //修改教师是否删除标志位

         //* 2.是否有此教师id查询教师任课表，将此条信息修改其标志
        TeachersSchedulesDao teachersSchedulesDao = new TeachersSchedulesDaolmpl();
        TeachersSchedules teachersSchedules = new TeachersSchedules();
        teachersSchedules.setTeacherid(teacher1.getTeacherid());


        List<TeachersSchedules> teachersSchedules1 = teachersSchedulesDao.queryByteacherid01(teachersSchedules);



        for (TeachersSchedules teachersSchedules2:teachersSchedules1) {



            //修改其标志
            teachersSchedules2.setIsDelete(1);
            int update = teachersSchedulesDao.update(teachersSchedules2);


            //   * 3.选课临时表包含教师任课表的id，将此条信息修改其标志
            TemporaryClassTable temporaryClassTable = new TemporaryClassTable();
            temporaryClassTable.setTeachersschedulesid(teachersSchedules2.getTeachersschedulesid());
            TemporaryClassTableDaolmpl temporaryClassTableDaolmpl = new TemporaryClassTableDaolmpl();
            TemporaryClassTable temporaryClassTable1 = temporaryClassTableDaolmpl.queryByTeachersschedulesid(temporaryClassTable);

            if (temporaryClassTable1!=null){
                temporaryClassTable1.setIsDelete(1);
                int update1 = temporaryClassTableDaolmpl.update(temporaryClassTable1);
            }
        }

        ClassandgradDaolmpl classandgradDaolmpl = new ClassandgradDaolmpl();
        Classandgrade classandgrade = new Classandgrade();
        classandgrade.setTeacherid(teacher1.getTeacherid());

        //查询班级信息
        Classandgrade classandgrade1 = classandgradDaolmpl.queryTeacherId(classandgrade);
        if(classandgrade1!=null){
            classandgrade1.setTeacherid(11);
            int update1 = classandgradDaolmpl.update(classandgrade1);
        }
        int update = update(teacher1);
        return update;
    }


    /**
     * 查询所有教师信息
     * @return 所有教师信息
     */
    @Override
    public List<Teacher> queryAll() {
        Teacher teacher = new Teacher();
        teacher.setIsDelete(0);
        return teacherDao.queryAll(teacher);
    }


    /**
     * 根据教师ID查询教师信息
     * @param teacher 教师ID
     * @return 教师信息
     */
    @Override
    public Teacher queryById(Teacher teacher) {
        return teacherDao.queryById(teacher);
    }

    @Override
    public Integer queryTeacherCount() {
        return teacherDao.queryForPageTotalCount();
    }

}
